layui.use(['form','tree','element','table','laytpl','layer','jquery'],function(){
    var form = layui.form,
        element = layui.element;
    $ = layui.$;
    var layer = parent.layer === undefined ? layui.layer : top.layer;
    var table = layui.table;
    var laytpl = layui.laytpl;
    var layedit = layui.layedit;

    //栏目内容列表
    var tableIns = table.render({
        elem: '#tableList',
        url : '/content/page',
        cellMinWidth : 95,
        page : true,
        height : "full-135",
        limits : [10,15,20,25],
        limit : 10,
        id : "listTable",
        cols : [[
            {type: "checkbox", fixed:"left", width:50,style:'height:115px;line-height:115px;'},
            {field: 'title', title: '标题名称', minWidth:100, align:"center"},
            {field: 'pic', title: '内容图片', minWidth:200,style:'height:80px;', align:'center',templet:function(d){
                    if (d.pic != null && d.pic != "") {
                        return '<img src="'+ d.pic +'" width="100" />';
                    }else {
                        return '';
                    }
                }},
            {field: 'url', title: '链接',align:'center',templet:function(d){
                    //处理时间
                    return d.url != null? d.url : '';
                }},
            {field: 'categoryId', title: '所属栏目分类', align:'center',templet:function(d){
                    var categoryName = "";
                    $.ajax({
                        url : '/contentCatogory/detail/' + d.categoryId,
                        async:false,
                        success: function (data) {
                            categoryName = data.name;
                        }
                    });
                    return categoryName == null?'':categoryName;
                }},
            {field: 'categoryId', title: '栏目分类ID', align:'center',templet:function(d){
                    return d.categoryId == null?'':d.categoryId;
                }},
            /*{field: 'isShow', title: '是否展示',  align:'center',templet:function(d){
                    return d.isShow == "1" ? "是" : "否";
                }},*/
            {field: 'createDate', title: '添加时间', sort: true,align:'center',templet:function(d){
                    //处理时间
                    var date = new Date(d.createDate);
                    return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
                }},
            {title: '操作', minWidth:175,fixed:"right",style:'height:115px;line-height:115px;', templet:'#tableListBar',align:"center"}
        ]]
    });

    //栏目内容分类数据
    var height = $("#demo2-view").height();
    $(".orgbox").css('height',height + "px");
    var treeData = {
        elem: '#tree' //指定元素
        , target: '_blank' //是否新选项卡打开（比如节点返回href才有效）
        , click: function (item) { //点击节点回调
            table.render({
                elem: '#tableList',
                url : '/content/query?categoryId=' + item.id,
                cellMinWidth : 95,
                page : true,
                height : "full-135",
                limits : [10,15,20,25],
                limit : 10,
                id : "listTable",
                cols : [[
                    {type: "checkbox", fixed:"left", width:50,style:'height:115px;line-height:115px;'},
                    {field: 'title', title: '标题名称', minWidth:100, align:"center"},
                    {field: 'pic', title: '内容图片', minWidth:200,style:'height:80px;', align:'center',templet:function(d){
                            if (d.pic != null && d.pic != "") {
                                return '<img src="'+ d.pic +'" width="100" />';
                            }else {
                                return '';
                            }
                        }},
                    {field: 'url', title: '链接',align:'center',templet:function(d){
                            //处理时间
                            return d.url != null? d.url : '';
                        }},
                    {field: 'categoryId', title: '所属栏目分类', align:'center',templet:function(d){
                            var categoryName = "";
                            $.ajax({
                                url : '/contentCatogory/detail/' + d.categoryId,
                                async:false,
                                success: function (data) {
                                    categoryName = data.name;
                                }
                            });
                            return categoryName == null?'':categoryName;
                        }},
                    {field: 'categoryId', title: '栏目分类ID', align:'center',templet:function(d){
                            return d.categoryId == null?'':d.categoryId;
                        }},
                    /*{field: 'isShow', title: '是否展示',  align:'center',templet:function(d){
                            return d.isShow == "1" ? "是" : "否";
                        }},*/
                    {field: 'createDate', title: '添加时间', sort: true,align:'center',templet:function(d){
                            //处理时间
                            var date = new Date(d.createDate);
                            return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
                        }},
                    {title: '操作', minWidth:175,fixed:"right",style:'height:115px;line-height:115px;', templet:'#tableListBar',align:"center"}
                ]]
            });
        },
        nodes:null
    };
    //异步加载栏目内容分类数据
    $.get("/contentCatogory/tree",function(data) {
        treeData.nodes = data.data;
        layui.tree(treeData);
    });

    //搜索栏目内容
    $(".search_btn").on("click",function(){
        var searchKey = $(".searchVal").val();
        if(searchKey != ''){
            table.render({
                elem: '#tableList',
                url : '/content/query?title=' + searchKey,
                cellMinWidth : 95,
                page : true,
                height : "full-135",
                limits : [10,15,20,25],
                limit : 10,
                id : "listTable",
                cols : [[
                    {type: "checkbox", fixed:"left", width:50,style:'height:115px;line-height:115px;'},
                    {field: 'title', title: '标题名称', minWidth:100, align:"center"},
                    {field: 'pic', title: '内容图片', minWidth:200,style:'height:80px;', align:'center',templet:function(d){
                            if (d.pic != null && d.pic != "") {
                                return '<img src="'+ d.pic +'" width="100" />';
                            }else {
                                return '';
                            }
                        }},
                    {field: 'url', title: '链接',align:'center',templet:function(d){
                            //处理时间
                            return d.url != null? d.url : '';
                        }},
                    {field: 'categoryId', title: '所属栏目分类', align:'center',templet:function(d){
                            var categoryName = "";
                            $.ajax({
                                url : '/contentCatogory/detail/' + d.categoryId,
                                async:false,
                                success: function (data) {
                                    categoryName = data.name;
                                }
                            });
                            return categoryName == null?'':categoryName;
                        }},
                    {field: 'categoryId', title: '栏目分类编号', align:'center',templet:function(d){
                            return d.categoryId == null?'':d.categoryId;
                        }},
                    /*{field: 'isShow', title: '是否展示',  align:'center',templet:function(d){
                            return d.isShow == "1" ? "是" : "否";
                        }},*/
                    {field: 'createDate', title: '添加时间', sort: true,align:'center',templet:function(d){
                            //处理时间
                            var date = new Date(d.createDate);
                            return date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
                        }},
                    {title: '操作', minWidth:175,fixed:"right",style:'height:115px;line-height:115px;', templet:'#tableListBar',align:"center"}
                ]]
            });
        }else{
            layer.msg("请输入搜索的内容");
        }
    });

    //添加栏目内容
    function addUser(edit){
        var index = layui.layer.open({
            title : "添加栏目内容",
            type : 2,
            content : "content_add.html",
            area: ['100%',$(document).height() + 'px'],
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                if(edit){
                    body.find("#pid").val(edit.pid);
                    body.find("#pname").val(edit.pname);
                    body.find("#isShow").val(edit.isShow);
                    body.find("#tree-select").val(edit.category);
                    form.render();
                }
                setTimeout(function(){
                    layui.layer.tips('点击此处返回栏目内容列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        });
        layui.layer.full(index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(index);
        })
    }
    $(".addBtn").click(function(){
        addUser();
    });

    //批量删除
    $(".delAll_btn").click(function(){
        var checkStatus = table.checkStatus('listTable'),//此处的id为table数据格式中的id
            data = checkStatus.data,
            idArr = [];
        if(data.length > 0) {
            for (var i in data) {
                idArr.push(data[i].id);
            }
            layer.confirm('确定删除选中的栏目内容？', {icon: 3, title: '提示信息'}, function (index) {
                for(var i=0;i<idArr.length;i++){
                    $.get("/content/delete?id=" + idArr[i],function(data){
                        if (data.code == 200) {
                            layer.msg("删除成功！");
                        } else {
                            layer.msg("删除失败，请重试！");
                        }
                        tableIns.reload();
                        layer.close(index);
                    });
                }
            })
        }else{
            layer.msg("请选择需要删除的栏目内容");
        }
    });

    //列表操作
    table.on('tool(tableList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;
        if(layEvent === 'edit'){ //编辑
            var index = layui.layer.open({
                title : "修改栏目内容",
                type : 2,
                content : "content_edit.html?id=" + data.id,
                area: ['100%',($(document).height() - 40) + 'px'],
                success : function(){
                    setTimeout(function(){
                        layui.layer.tips('点击此处返回栏目内容列表', '.layui-layer-setwin .layui-layer-close', {
                            tips: 3
                        });
                    },500);
                }
            });
            layui.layer.full(index);
            //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
            $(window).on("resize",function(){
                layui.layer.full(index);
            })
        }else if(layEvent === 'del'){ //删除
            layer.confirm('确定删除此栏目内容？',{icon:3, title:'提示信息'},function(index){
                $.get("/content/delete?id=" + data.id,function(data){
                    if (data.code == 200) {
                        layer.msg("删除成功！");
                    } else {
                        layer.msg("删除失败，请重试！");
                    }
                    tableIns.reload();
                    layer.close(index);
                });
            });
        }
    });


});